<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/22
 * Time: 13:32
 */

namespace Admin\Controller;


class AuditDataController extends AuthController{

    protected $table;
    protected $category;
    protected $type;
    protected $list;


    public function __construct(){
         parent::__construct();

         $this->table=D('file');
         $this->category=D('category');

        $this->type='90';
        $this->list='20';

    }

    public function index(){


        $this->assign('allFile',$this->table->all_file($this->list));

        $this->del_upload_runtime();

        $this->display('index');

    }

    public function search(){


        $sql=$this->table->search_file($this->type,I('post.name'));



        if($sql){
            $this->ajaxReturn($sql);
        }else{
            $this->ajaxReturn('无法搜索到您要的结果');
        }

    }


    public function audit(){

    $this->assign('allFile',$this->table->auditor_file($this->list));

        $this->display();
    }

    public function edit(){

        if(IS_GET){
           $sql = $this->table->once_file(I('get.id'));
           if($sql){
               $this->assign('file',$sql);
           }else{
               die('不存在');
           }
        }

        if(IS_POST){
            $data=array();
            $post = I('post.');
            $data['auditor'] =$post['auditor'];
            $data['id'] =$post['code'];
            $data['file_category'] =$post['file_category_id'];
            $data['content'] =$post['content'];

            if($this->table->create($data)){
                if($this->table->save($data)){

                    $this->success('审核成功',U('AuditData/index'));
                }else{

                   $this->error('失败');
                }

            }else{
                $this->error('22');
            }

        }


        $this->assign('category',$this->category->all_category($this->type));
        $this->display();
    }





    public function category(){


        $category = $this->category->all_category($this->type);

        $this->assign('category',$category);

        $this->display();
    }

    public function add_category(){

        if(IS_POST){
            if($this->category->create()){

                if($this->category->add()){

                    $this->success('添加成功',U('AuditData/category'));

                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($this->category->getError());
            }
        }

        $all_category =$this->category->where('category_type=%d',$this->type)->select();
        $this->assign('category',$all_category);
        $this->display();
    }
    public function edit_category(){

        if(IS_GET){

            $edit_val = $this->category->edit(I('get.key'),I('get.id'));

            if($edit_val){
                $this->assign('category',$edit_val);
            }else {
                $this->error('数据不存在！！！');
            }

        }


        if(IS_POST){

            if($this->category->create()){

                if($this->category->save()){

                    $this->success('更新成功');
                }else{
                   $this->error('更新失败');
                }
            }else{

                $this->error($this->category->getError());
            }

        }


        $this->assign('all_category',$this->category->all_category($this->type));

        $this->display();
    }





    public function del_category(){

        if(IS_GET){

            $sql =$this->category->where('id=%d AND category_type=%d',array(I('get.id'),I('get.key')))->delete();

            if($sql){
                $this->success('删除成功',U('AuditData/category'));
            }else{

                $this->error('删除失败');
            }
        }

    }

    public function del_upload_runtime(){

        $dir ='./Public/upload_Runtime/';
       $dir_array = scandir($dir,true);

       foreach ($dir_array as $key=>$val){
           rmdir($dir.$val);
       }
       $date =date('Y-m-d',time()-(7*24*60*60));

       if(in_array($date,$dir_array)){
            $file =$dir.$date;
            $file_array =scandir($file,true);
            foreach ($file_array as $key=>$val){
                unlink($file.'/'.$val);
            }
       }

    }

    public function download(){
        $public ='./Public/';

        if (IS_GET) {

            $file=$this->table->once_file(I('get.id'));

            $file['file_url'] =$public.$file['file_url'];
            if (file_exists(iconv('utf-8', 'gbk',$file['file_url']))) {

                $down = new \Common\Common\Download();

                $down->load($file);
            }
        }
    }





}